/*
 * Date         Author      Note
 * 2014.11.24   wzwz        帮助窗口
 * 2015.06.24   wzwz        添加条目
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "mhbtn.h"
#include "help.h"
#include "com.h"
#include "extent.h"
#include "speed.h"
#include "rab.h"
#include "roll.h"
#include "switch.h"
#include "yarn_stop.h"
#include "yarn_exchange.h"
#include "yarn_check.h"
#include "economy.h"
#include "stop_line.h"
#include "knit_line.h"
#include "density.h"
#include "version.h"

#define HELP_ITEM_LEFT       (CX_EDGE_LEFT * 2)
#define HELP_ITEM_TOP        (CY_TITLE4_HEIGHT + CX_EDGE_LEFT)
#define HELP_ITEM_WIDTH      (130)
#define HELP_ITEM_HEIGHT     (27)
#define HELP_ITEM_INTER      (10)
/*
 *
 */
static void CreateHelpInterface(HWND hWnd)
{
    int i = 0;
    HWND hWndTmp = -1;
    //RESET
    hWndTmp = CreateWindow (MH_BTN_NAME,
       MW_HELP_RESET_CAPTION,
       WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
       ID_BTN_RESET,
       HELP_ITEM_LEFT, HELP_ITEM_TOP, HELP_ITEM_WIDTH, HELP_ITEM_HEIGHT,
       hWnd, MH_BTN_BKGND_BLACK);
    //HD
    hWndTmp = CreateWindow (MH_BTN_NAME,
        MW_HELP_HD_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_BTN_HD,
        HELP_ITEM_LEFT, HELP_ITEM_TOP + HELP_ITEM_HEIGHT, HELP_ITEM_WIDTH, HELP_ITEM_HEIGHT,
        hWnd, MH_BTN_BKGND_BLACK);
    //speed
    hWndTmp = CreateWindow (MH_BTN_NAME,
        MW_HELP_SPEED_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_BTN_SPEED,
        HELP_ITEM_LEFT, HELP_ITEM_TOP + HELP_ITEM_HEIGHT * 2, HELP_ITEM_WIDTH, HELP_ITEM_HEIGHT,
        hWnd, MH_BTN_BKGND_BLACK);
    //piece lock
    hWndTmp = CreateWindow (MH_BTN_NAME,
        MW_HELP_PIECE_LOCK_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_PIECE_LOCK,
        HELP_ITEM_LEFT, HELP_ITEM_TOP + HELP_ITEM_HEIGHT * 3, HELP_ITEM_WIDTH, HELP_ITEM_HEIGHT,
        hWnd, MH_BTN_BKGND_BLACK);
    //warn enable
    hWndTmp = CreateWindow (MH_BTN_NAME,
        MW_HELP_WARN_ENABLE_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_WARN_ENABLE,
        HELP_ITEM_LEFT, HELP_ITEM_TOP + HELP_ITEM_HEIGHT * 4, HELP_ITEM_WIDTH, HELP_ITEM_HEIGHT,
        hWnd, MH_BTN_BKGND_BLACK);
    //yarn up
    hWndTmp = CreateWindow (MH_BTN_NAME,
        MW_HELP_YARN_UP_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_YARN_UP,
        HELP_ITEM_LEFT, HELP_ITEM_TOP + HELP_ITEM_HEIGHT * 5, HELP_ITEM_WIDTH, HELP_ITEM_HEIGHT,
        hWnd, MH_BTN_BKGND_BLACK);
    //help
    hWndTmp = CreateWindow (MH_BTN_NAME,
        MW_HELP_ITEM_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_HELP,
        HELP_ITEM_LEFT, HELP_ITEM_TOP + HELP_ITEM_HEIGHT * 6, HELP_ITEM_WIDTH, HELP_ITEM_HEIGHT,
        hWnd, MH_BTN_BKGND_BLACK);
    //spread
    hWndTmp = CreateWindow (MH_BTN_NAME,
        MW_HELP_SPREAD_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_SPREAD,
        HELP_ITEM_LEFT, HELP_ITEM_TOP + HELP_ITEM_HEIGHT * 7, HELP_ITEM_WIDTH, HELP_ITEM_HEIGHT,
        hWnd, MH_BTN_BKGND_BLACK);
    //dumu
    hWndTmp = CreateWindow (MH_BTN_NAME,
        MW_HELP_DUMU_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_DUMU,
        HELP_ITEM_LEFT, HELP_ITEM_TOP + HELP_ITEM_HEIGHT * 8, HELP_ITEM_WIDTH, HELP_ITEM_HEIGHT,
        hWnd, MH_BTN_BKGND_BLACK);
    //mast roll
    hWndTmp = CreateWindow (MH_BTN_NAME,
        MW_HELP_MAST_ROLL_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_MAST_ROLL,
        HELP_ITEM_LEFT, HELP_ITEM_TOP + HELP_ITEM_HEIGHT * 9, HELP_ITEM_WIDTH, HELP_ITEM_HEIGHT,
        hWnd, MH_BTN_BKGND_BLACK);
    //aux_roll
    hWndTmp = CreateWindow (MH_BTN_NAME,
       MW_HELP_AUX_ROLL_CAPTION,
       WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
       ID_AUX_ROLL,
       HELP_ITEM_LEFT + (HELP_ITEM_WIDTH + HELP_ITEM_INTER),
       HELP_ITEM_TOP, HELP_ITEM_WIDTH, HELP_ITEM_HEIGHT,
       hWnd, MH_BTN_BKGND_BLACK);
    //switch
    hWndTmp = CreateWindow (MH_BTN_NAME,
        MW_HELP_SWITCH_ROLL_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_SWITCH_ROLL,
        HELP_ITEM_LEFT + (HELP_ITEM_WIDTH + HELP_ITEM_INTER),
        HELP_ITEM_TOP + HELP_ITEM_HEIGHT, HELP_ITEM_WIDTH, HELP_ITEM_HEIGHT,
        hWnd, MH_BTN_BKGND_BLACK);
    //speed
    hWndTmp = CreateWindow (MH_BTN_NAME,
        MW_HELP_SPEED_SET_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_SPEED,
        HELP_ITEM_LEFT + (HELP_ITEM_WIDTH + HELP_ITEM_INTER),
        HELP_ITEM_TOP + HELP_ITEM_HEIGHT * 2, HELP_ITEM_WIDTH, HELP_ITEM_HEIGHT,
        hWnd, MH_BTN_BKGND_BLACK);
    //yarn stop
    hWndTmp = CreateWindow (MH_BTN_NAME,
        MW_HELP_YARN_STOP_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_YARN_STOP,
        HELP_ITEM_LEFT + (HELP_ITEM_WIDTH + HELP_ITEM_INTER),
        HELP_ITEM_TOP + HELP_ITEM_HEIGHT * 3, HELP_ITEM_WIDTH, HELP_ITEM_HEIGHT,
        hWnd, MH_BTN_BKGND_BLACK);
    //goto line
    hWndTmp = CreateWindow (MH_BTN_NAME,
        MW_HELP_GOTO_LINE_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_GOTO_LINE,
        HELP_ITEM_LEFT + (HELP_ITEM_WIDTH + HELP_ITEM_INTER),
        HELP_ITEM_TOP + HELP_ITEM_HEIGHT * 4, HELP_ITEM_WIDTH, HELP_ITEM_HEIGHT,
        hWnd, MH_BTN_BKGND_BLACK);
    //yarn pos
    hWndTmp = CreateWindow (MH_BTN_NAME,
        MW_HELP_YARN_POS_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_YARN_POS,
        HELP_ITEM_LEFT + (HELP_ITEM_WIDTH + HELP_ITEM_INTER),
        HELP_ITEM_TOP + HELP_ITEM_HEIGHT * 5, HELP_ITEM_WIDTH, HELP_ITEM_HEIGHT,
        hWnd, MH_BTN_BKGND_BLACK);
    //yarn exchange
    hWndTmp = CreateWindow (MH_BTN_NAME,
        MW_HELP_YARN_EXCHANGE_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_HELP,
        HELP_ITEM_LEFT + (HELP_ITEM_WIDTH + HELP_ITEM_INTER),
        HELP_ITEM_TOP + HELP_ITEM_HEIGHT * 6, HELP_ITEM_WIDTH, HELP_ITEM_HEIGHT,
        hWnd, MH_BTN_BKGND_BLACK);
    //yarn com for intasia
    hWndTmp = CreateWindow (MH_BTN_NAME,
        MW_HELP_YARN_COM_INTASIA_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_SPREAD,
        HELP_ITEM_LEFT + (HELP_ITEM_WIDTH + HELP_ITEM_INTER),
        HELP_ITEM_TOP + HELP_ITEM_HEIGHT * 7, HELP_ITEM_WIDTH, HELP_ITEM_HEIGHT,
        hWnd, MH_BTN_BKGND_BLACK);
    //yarn pos for intasia
    hWndTmp = CreateWindow (MH_BTN_NAME,
        MW_HELP_YARN_STOP_INTASIA_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_DUMU,
        HELP_ITEM_LEFT + (HELP_ITEM_WIDTH + HELP_ITEM_INTER),
        HELP_ITEM_TOP + HELP_ITEM_HEIGHT * 8, HELP_ITEM_WIDTH, HELP_ITEM_HEIGHT,
        hWnd, MH_BTN_BKGND_BLACK);
    //cycle
    hWndTmp = CreateWindow (MH_BTN_NAME,
        MW_HELP_CYCLE_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_ECONOMY,
        HELP_ITEM_LEFT + (HELP_ITEM_WIDTH + HELP_ITEM_INTER),
        HELP_ITEM_TOP + HELP_ITEM_HEIGHT * 9, HELP_ITEM_WIDTH, HELP_ITEM_HEIGHT,
        hWnd, MH_BTN_BKGND_BLACK);
    //D
    const char *edit3[10] = {
        "D", GetString(160), GetString(161), GetString(162), GetString(163),
        GetString(798), GetString(799), GetString(800), GetString(801),
    };
    ushort nID3[10] = {
            ID_RSVD, ID_BOARD_TEST, ID_STOP_LINE, ID_VERSION, ID_BTN_BACK,
            ID_YARN_SPEED, ID_MAC_BEFORE, ID_YARN_BEFORE, ID_YARN_FEEDER,
    };
    for(i = 0; i < 9; ++i)
    {
        CreateWindow (MH_BTN_NAME,
           edit3[i],
           WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
           nID3[i],
           20 + 140 * 2,
           63 + 27 * i, 136, 27,
           hWnd, MH_BTN_BKGND_BLACK);
    }
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    switch(nID)
    {
    case ID_BTN_BACK:
        {
            PostMessage (hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    case ID_BTN_RESET:
        break;
    case ID_BTN_HD:
        break;
    case ID_BTN_SPEED:
        break;
    case ID_PIECE_LOCK:
        break;
    case ID_WARN_ENABLE:
        break;
    case ID_YARN_UP:
        break;
    case ID_HELP:
        break;
    case ID_SPREAD:
        break;
    case ID_DUMU:
        break;
    case ID_MAST_ROLL:
        break;
    case ID_AUX_ROLL:
        break;
    case ID_SWITCH_ROLL:
        break;
    case ID_YARN_STOP:
        break;
    case ID_GOTO_LINE:
        break;
    case ID_YARN_POS:
        break;
    case ID_YARN_EXCHANGE:
        break;
    case ID_ECONOMY:
        break;
    case ID_BOARD_TEST:
        break;
    case ID_STOP_LINE:
        break;
    case ID_VERSION:
        break;
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, int key)
{
    if(key == SCANCODE_ESCAPE)
    {
        PostMessage (hWnd, MSG_CLOSE, 0, 0);
    }
    else if(key == SCANCODE_F1)
    {
        //OnReset()
    }
    else if(key == SCANCODE_F2)
    {
        //OnHangding()
    }
    else if(key == SCANCODE_F3)
    {
        //OnSpeedSwitch()
    }
    else if(key == SCANCODE_F4)
    {
        //OnPieceLock()
    }
    else if(key == SCANCODE_F5)
    {
        //OnWarnEnable()
    }
    else if(key == SCANCODE_F6)
    {
        //OnYarnUp()
    }
    else if(key == SCANCODE_F7)
    {
        //OnHelp()
    }
    else if(key == SCANCODE_0)
    {
        //CreateExtentWindow(hWnd);
    }
    else if(key == SCANCODE_1)
    {
        //CreateDensityWindow(hWnd);
    }
    else if(key == SCANCODE_2)
    {
        //CreateRabWindow(hWnd);
    }
    else if(key == SCANCODE_3)
    {
        //CreateRollWindow(hWnd);
    }
    else if(key == SCANCODE_4)
    {
        //CreateSwitchWindow(hWnd);
    }
    else if(key == SCANCODE_5)
    {
        //CreateSpeedWindow(hWnd);
    }
    else if(key == SCANCODE_6)
    {
        //CreateYarnStopWindow(hWnd);
    }
    else if(key == SCANCODE_7)
    {
        //CreateKnitLineWindow(hWnd);
    }
    else if(key == SCANCODE_8)
    {
        //CreateYarnCheckWindow(hWnd);
    }
    else if(key == SCANCODE_9)
    {
        //CreateYarnExchangeWindow(hWnd);
    }
    else if(key == SCANCODE_C)
    {
        //CreateEconomyWindow(hWnd);
    }
    else if(key == SCANCODE_F)
    {
        //CreateStopLineWindow(hWnd);
    }
    else if(key == SCANCODE_PERIOD)
    {
        //CreateVersionWindow(hWnd);
    }
}
/*
 *
 */
static int HelpWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateHelpInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 100;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 100;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_CYAN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting > 0)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_KEYDOWN:
        OnKeyDown(hWnd, wParam);
        break;
    case MSG_CHAR:
        break;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_HELP]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateHelpWindow(HWND hHosting)
{
    int x = (CX_SCREEN - CX_HELP_WND_BMP) / 2;
    int y = (CY_SCREEN - CY_HELP_WND_BMP) / 2;
    
    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = "Help";
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = HelpWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_HELP_WND_BMP;
    CreateInfo.by = y + CY_HELP_WND_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        EnableWindow(hHosting, FALSE);
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
